package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hotel.domain.HmServiceRequest;
import com.ruoyi.hotel.domain.bo.HmWorkRateBo;
import com.ruoyi.hotel.mapper.HmServiceRequestMapper;
import com.ruoyi.hotel.service.IHmTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmTaskBo;
import com.ruoyi.hotel.domain.vo.HmTaskVo;
import com.ruoyi.hotel.domain.HmTask;
import com.ruoyi.hotel.mapper.HmTaskMapper;
import com.ruoyi.hotel.service.IHmTaskService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmTaskServiceImpl implements IHmTaskService {

    private final HmTaskMapper baseMapper;
    private final HmServiceRequestMapper hmServiceRequestMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public HmTaskVo queryById(Long taskId){
        return baseMapper.selectVoById(taskId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<HmTaskVo> queryPageList(HmTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmTask> lqw = buildQueryWrapper(bo);
        Page<HmTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<HmTaskVo> queryList(HmTaskBo bo) {
        LambdaQueryWrapper<HmTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmTask> buildQueryWrapper(HmTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmTask> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWorkerId() != null, HmTask::getWorkerId, bo.getWorkerId());
        lqw.eq(bo.getSupplyId() != null, HmTask::getSupplyId, bo.getSupplyId());
        lqw.eq(bo.getSupplyAmount() != null, HmTask::getSupplyAmount, bo.getSupplyAmount());
        lqw.eq(bo.getIsDone() != null, HmTask::getIsDone, bo.getIsDone());
        lqw.eq(bo.getServiceRequestId() != null, HmTask::getServiceRequestId, bo.getServiceRequestId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(HmTaskBo bo) {
        HmTask add = BeanUtil.toBean(bo, HmTask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTaskId(add.getTaskId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(HmTaskBo bo) {
        HmTask update = BeanUtil.toBean(bo, HmTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmTask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public HmTask selectOne(Long id){
        return baseMapper.selectById(id);
    }

    @Override
    public void updateById(HmTask task){
        baseMapper.updateById(task);
    }

    /**
     * 后勤反馈任务完成程度并评分
     * @param hmWorkRate
     */
    @Override
    public void taskDone(HmWorkRateBo hmWorkRate){

        HmTask task=baseMapper.selectById(hmWorkRate.getTaskId());
        task.setIsDone("d");
        baseMapper.updateById(task);

        HmServiceRequest serviceRequest=new HmServiceRequest();
        serviceRequest=hmServiceRequestMapper.selectById(task.getServiceRequestId());
        serviceRequest.setIsDone("y");
        hmServiceRequestMapper.updateById(serviceRequest);

    }

}
